package dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import util.Utilities;

import dto.BoardDTO;

public class BoardDAO {
	private SqlMapClientTemplate sqlMap;
	
	public BoardDAO(SqlMapClientTemplate sqlMap){
		this.sqlMap = sqlMap;
	}
	
	public int selectBoardNo(){
		return (Integer) sqlMap.queryForObject("board.selectNo");
	}
	
	public void insetBoard(BoardDTO bdto){
		sqlMap.insert("board.insert",bdto);
	}
	
	public List<BoardDTO> selectAllBoard() throws Exception{
		return (ArrayList<BoardDTO>) sqlMap.queryForList("board.selectAllBoard");
	}

	public List<BoardDTO> selectAllBoardNoti() throws Exception{
		return (ArrayList<BoardDTO>) sqlMap.queryForList("board.selectAllBoardNoti");
	}
	
	public BoardDTO boardContent(int no) {
		return (BoardDTO) sqlMap.queryForObject("board.selectContent",no);
	}
	
	public void plusViewCount(int no){
		sqlMap.update("board.plusViewCount",no);
	}
	
	public void deleteBoard(int no){
		sqlMap.delete("board.delete", no);
	}
	
	public void updateBoard(BoardDTO bdto){
		sqlMap.update("board.update", bdto);
	}
	
	public void replyBoard(BoardDTO bdto){
		sqlMap.update("board.restep",bdto);
	}
	
	public List<BoardDTO> paging(int page, String contentGroup){
		HashMap map = new HashMap();
		map.put("page", page);
		map.put("perPage", Utilities.CONTENT_PER_PAGE);
		map.put("contentGroup", contentGroup);
		
		return (ArrayList<BoardDTO>) sqlMap.queryForList("board.paging",map);
	}
	
	public int pageNum(String contentGroup){
		return (Integer) sqlMap.queryForObject("board.selectPagingNum", contentGroup);
	}
	
	public int pageNumNoti(){
		return (Integer) sqlMap.queryForObject("board.selectPagingNumNoti");
	}
	
	public List<BoardDTO> pagingSearch(int page, BoardDTO bdto){
		HashMap map = new HashMap();
		map.put("page", page);
		map.put("perPage", Utilities.CONTENT_PER_PAGE);
		map.put("contentGroup", bdto.getContentGroup());
		map.put("content", bdto.getContent());
		map.put("title", bdto.getTitle());
		
		return sqlMap.queryForList("board.paging", map);
	}
}
